/**
 *	users流程模块接口
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.system.usera;



import com.framerock.baselib.CDate;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.ACore.Model;
import com.framework.Flow.FlowListener;
import com.framework.Model.ModelDept;

import java.util.Map;

public class FlowListenerusera extends FlowListener {



    public Boolean onModejs() {
        return true;
    }

    public Boolean onRemind() { return false;}

    public Map<String, String> onDataafter()
    {
        Map<String, String> da = Rock.getMap();
        if(mRequest.COMPANYBOOL && mRequest.LOADCI==1) {
            da.put("deptarr", Json.getListString(ModelDept.get(this.nDb).getAll("`cid`="+mRequest.COMPANYID+"", "`id`,`name`,`pid`", "`sort`")));
        }
        return da;
    }


    public Map<String, String> onSavebefore(Map<String, String> da, String mid, Boolean isadd)
    {
        Map<String, String> map = Rock.getMap();
        if(isadd){
            map.put("savefields", "uid=0");
        }else {
            int state = Rock.getMapint(mFlow.data, "state");
            int uid   = Rock.getMapint(mFlow.data, "uid");
            if(state==1){
                map.put("notsave", "mobile");
            }
        }
        String mobile = Rock.getMapString(da, "mobile");
        if(!Rock.isEmpt(mobile)){
            Model uobj  = Model.get(this.nDb, "users");
            Map<String, String> urs = uobj.getOne("`mobile`='"+mobile+"'");
            if(urs != null){
                map.put("savefields", "uid="+urs.get("id")+",state=1");
            }else{
                //创建平台用户
                String rand = CDate.gettime();
                int uid = uobj.insert(Rock.getMap(new String[]{
                        "user", da.get("user"),
                        "name", da.get("name"),
                        "type", "0",
                        "mobile",mobile,
                        "rand", rand,
                        "pass", Jiami.md5("123456"+rand),
                        "adddt", CDate.now(),
                        "dcid", mRequest.COMPANYID
                }));
                map.put("savefields", "uid="+uid+",state=1");
            }
        }
        return map;
    }

    //更新人员路径



    public void onSaveafter(Map<String, String> da, String mid, Boolean isadd) {
        String deptid = da.get("deptid");

    }

    public Map<String, String> onDatabefore(){
        String deptid   = mRequest.get("deptid");
        String where    = "";
        if(!Rock.isEmpt(deptid)){
            String deptidall = ModelDept.get(this.nDb).getDeptdown(deptid);
            where = "`deptid` IN("+deptidall+")";
        }
        return Rock.getMap(new String[]{
                "where", where,
        });
    }
}
